package com.alan.base.config;

import android.content.Context;
import android.content.SharedPreferences;

import com.alan.base.App;

/**
 * SharedPreferences 管理类
 */
public class ConfigManager {

    // SharedPreferences 实例
    private SharedPreferences sp;
    // ConfigManager 实例
    private static ConfigManager manager;

    // log日志
    private static final String KEY_LOG_FILE_NAME = "key_log_file_name";

    /**
     * 构造方法
     */
    private ConfigManager() {
        if (App.getInstance().getApplication() != null) {
            sp = App.getInstance().getApplication().getSharedPreferences("alan_base_config", Context.MODE_PRIVATE);
        }
    }

    /**
     * 单例
     *
     * @return 返沪 ConfigManager 实例
     */
    public synchronized static ConfigManager getInstance() {
        if (null == manager || manager.sp == null) {
            synchronized (ConfigManager.class) {
                if (null == manager || manager.sp == null) {
                    manager = new ConfigManager();
                }
            }
        }
        return manager;
    }

    public void setLogFileName(String name) {
        if (sp != null) {
            sp.edit().putString(KEY_LOG_FILE_NAME, name).apply();
        }
    }

    public String getLogFileName() {
        if (sp != null) {
            return sp.getString(KEY_LOG_FILE_NAME, "");
        }
        return "";
    }

    public void put(String key, String value) {
        if (sp != null) {
            sp.edit().putString(key, value).apply();
        }
    }

    public void put(String key, int value) {
        if (sp != null) {
            sp.edit().putInt(key, value).apply();
        }
    }

    public void put(String key, boolean value) {
        if (sp != null) {
            sp.edit().putBoolean(key, value).apply();
        }
    }

    public void put(String key, long value) {
        if (sp != null) {
            sp.edit().putLong(key, value).apply();
        }
    }

    public void put(String key, float value) {
        if (sp != null) {
            sp.edit().putFloat(key, value).apply();
        }
    }

    public String get(String key) {
        return sp.getString(key, "");
    }

    public String get(String key, String def) {
        return sp.getString(key, def);
    }

    public int get(String key, int def) {
        return sp.getInt(key, def);
    }

    public float get(String key, float def) {
        return sp.getFloat(key, def);
    }

    public long get(String key, long def) {
        return sp.getLong(key, def);
    }

    public boolean get(String key, boolean def) {
        return sp.getBoolean(key, def);
    }
}
